// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'

import Axios from 'axios'

import echarts from 'echarts'

import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';

import {Loading} from 'element-ui';

Vue.prototype.$echarts = echarts;

Vue.config.productionTip = false;

Vue.use(ElementUI);


let loading;
//内存中正在请求的数量
let loadingNum=0;
function startLoading() {
  if(loadingNum==0){
    loading = Loading.service({
      lock: true,
      text: '拼命加载中...',
      background:'rgba(255,255,255,0.5)',
    })
  }
  //请求数量加1
  loadingNum++;
}

function endLoading() {
  //请求数量减1
  loadingNum--;
  if(loadingNum<=0){
    loading.close()
  }
}

//全局Axios拦截器
Axios.interceptors.request.use(config=>{
    startLoading();
    return config;
},error=>{
    return Promise.reject(error);
});

Axios.interceptors.response.use(response=>{
   endLoading();
   if(response.status == 505){
     router.replace({
       path: '/',
       query: {redirect: router.currentRoute.fullPath}
     })
   }else{
     return response;
   }
},error=>{
  endLoading();
  router.replace({
    path: '/',
    query: {redirect: router.currentRoute.fullPath}
  })
});

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
});
